package com.zhongwang.business.util;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  项目号转换工具类
 *
 * @author China.sgl
 * @date 2020/6/5 08:23
 */
public class CodeUtil {

    /**
     * 项目号转8位
     *
     * @param projectCode 项目号
     * @return 八位项目号
     */
    public static String projectCodeHandle(String projectCode) {
        String handleCode = "";
        try {
            if (!projectCode.trim().equals("")) {
                String beginStr = projectCode.trim().toCharArray()[0]+""+projectCode.trim().toCharArray()[1];
                char lastChar = projectCode.trim().toCharArray()[projectCode.trim().length()-1];
                if (beginStr.toUpperCase().equals("ZW")  || isNumeric(projectCode.trim()) || lastChar >='a' && lastChar <= 'z' || lastChar >='A' && lastChar <= 'Z') {
                    handleCode = projectCode.trim();
                } else {
                    if (projectCode.trim().length() == 8) {
                        handleCode = projectCode.trim().substring(2);
                    } else {
                        handleCode = projectCode.trim();
                    }
                    String year = new SimpleDateFormat("YYYY").format(new Date());
                    if (Integer.parseInt(year.substring(0, 2)) > 20) {
                        handleCode = year.substring(0, 2) + handleCode;
                    } else {
                        handleCode = "20" + handleCode;
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return projectCode.toUpperCase();
        }
        return handleCode.toUpperCase();
    }

    /**
     * 判断字符串是否全为数字
     *
     * @param str 字符串
     * @return boolean
     */
    private static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            int chr = str.charAt(i);
            if (chr < 48 || chr > 57) {
                return false;
            }
        }
        return true;
    }
}
